package com.dqp.cslggroup.ConnectSever;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dqp.cslggroup.bean.Version;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Iterator;

public class updateApp {

    public static Version nowVersion(int getthisVersion) {
        Version ver = new Version();
        try {
            Connection.Response response = Jsoup.connect("http://210.28.164.3/47.96.190.22/app/version.php")
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .timeout(10000)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                JSONArray appVersion = JSON.parseArray(jsonObject.getString("version"));
                if (appVersion != null && !appVersion.isEmpty()) {
                    Iterator iterator = appVersion.iterator();
                    JSONObject version = (JSONObject) iterator.next();
                    int versionCode = version.getIntValue("versionCode");
                    String versionName = version.getString("versionName");
                    String des = version.getString("describes");
                    String dates = version.getString("dates");
                    boolean update = false;
                    if (versionCode > getthisVersion) update = true;
                    ver.setNewVersion("最新版本：" + versionName);
                    ver.setUpdate(update);
                    ver.setInfo(des + "");
                    ver.setOldVersion(getthisVersion + "");
                    ver.setDate("发布时间：" + dates);
                }
            }
        } catch (IOException | Error e) {
            e.printStackTrace();
            return null;
        }
        return ver;
    }


    public static void call_app() {
        new Thread(() -> {
            try {
                Jsoup.connect("http://210.28.164.3/47.96.190.22/apppv/appcall.php")
                        .ignoreContentType(true) // 获取图片需设置忽略内容类型
                        .userAgent("Mozilla")
                        .method(Connection.Method.GET)
                        .timeout(3000)
                        .execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }


    public static void push_pv() {
        new Thread(() -> {
            try {
                Jsoup.connect("http://210.28.164.3/47.96.190.22/app/pushpv.php")
                        .ignoreContentType(true) // 获取图片需设置忽略内容类型
                        .userAgent("Mozilla")
                        .method(Connection.Method.GET)
                        .timeout(3000)
                        .execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }


}
